package com.zmc.product.controller;

import com.zmc.common.domain.vo.user.UserInfo;
import com.zmc.product.infrastructure.entity.CommodityGoodsSkuEntity;
import com.zmc.product.infrastructure.service.CommodityGoodsSkuService;
import com.zmc.product.exception.ProductNotExistException;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.entity.CommodityBannerEntity;
import com.zmc.product.infrastructure.bean.query.CommodityBannerQuery;
import com.zmc.product.infrastructure.service.CommodityBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 轮播图
 * @author zmc
 */
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private CommodityBannerService commodityBannerService;

    @Autowired
    private CommodityGoodsSkuService commodityGoodsSkuService;

    @Autowired
    private AuthTokenService authTokenService;

    @Value("${banner.to.path.prefix}")
    private String pathPrefix;


    /**
     * 查询轮播图列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CommodityBannerQuery query) {
        PageUtils page = commodityBannerService.pageCommodityBannerList(query);
        return R.page(page);
    }


    /**
     * 导出轮播图列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityBannerQuery query) {
        List<CommodityBannerEntity> list = commodityBannerService.commodityBannerList(query);
        ExcelUtil<CommodityBannerEntity> util = new ExcelUtil<>(CommodityBannerEntity.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @PostMapping( "/info")
    public R getInfo(@RequestBody CommodityBannerEntity commodityBanner) {
        return R.ok(commodityBannerService.getById(commodityBanner.getId()));
    }

    /**
     * 新增轮播图
     */
    @PostMapping("/add")
    public R add(@RequestBody CommodityBannerEntity commodityBanner) {
        CommodityGoodsSkuEntity skuEntity = commodityGoodsSkuService.getBySkuId(commodityBanner.getSkuId());
        if (skuEntity == null) {
            throw new ProductNotExistException();
        }
        commodityBanner.setToPath(pathPrefix + skuEntity.getSpuId() + "/" + skuEntity.getId());
        UserInfo userInfo = authTokenService.getUserInfo();
        commodityBanner.setCreateBy(userInfo.getUsername());
        commodityBannerService.save(commodityBanner);
        return R.ok();
    }

    /**
     * 修改轮播图
     */
    @PostMapping("/update")
    public R edit(@RequestBody CommodityBannerEntity commodityBanner) {
        CommodityGoodsSkuEntity skuEntity = commodityGoodsSkuService.getBySkuId(commodityBanner.getSkuId());
        if (skuEntity == null) {
            throw new ProductNotExistException();
        }
        commodityBanner.setToPath(pathPrefix + skuEntity.getSpuId() + "/" + skuEntity.getSkuId());
        UserInfo userInfo = authTokenService.getUserInfo();
        commodityBanner.setUpdateBy(userInfo.getUsername());
        commodityBannerService.updateById(commodityBanner);
        return R.ok();
    }

    /**
     * 删除轮播图
     */
    @PostMapping("/delete")
    public R remove(@RequestBody CommodityBannerEntity commodityBanner) {
        commodityBannerService.removeById(commodityBanner.getId());
        return R.ok();
    }
}
